package winOrDie.graphicsInterface.GameMenu;

import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;

import winOrDie.Game.WoD_Game;
import winOrDie.Net.Http.httpmsg.WoD_HttpMessage;

public class Composite_New_Game extends Composite {

	private Label label_New_Game = null;
	
	private Composite composite_Game_Title = null;
	private Label label_Game_Title = null;
	private Text text_Game_Title = null;
	
	private Composite composite_Buttons = null;
	private Button button_Accept = null;
	private Button button_Cancel = null;


	private Vector<Composite_New_Game_Listener> listeners = null;
	
	public synchronized void add_Composite_New_Game_Listener(Composite_New_Game_Listener listener)
	{
		listeners.add(listener);
	}

	public synchronized void remove_Composite_New_Game_Listener(Composite_New_Game_Listener listener)
	{
		listeners.remove(listener);
	}

	protected void fire_Composite_New_Game_Event(Composite_New_Game_Event event)
	{
		for (Composite_New_Game_Listener listener : listeners) {
			listener.onComposite_New_Game_Change(event);
		}
	}
	
	private WoD_Game woDGame = null;
	
	public WoD_Game getWoDGame() {
		return woDGame;
	}

	public void setWoDGame(WoD_Game woDGame) {
		this.woDGame = woDGame;
	}
	
	private void show_GameData()
	{
		text_Game_Title.setText(woDGame.getGame_Title());
	}
	
	private void fill_GameData()
	{
		woDGame.setGame_Title(text_Game_Title.getText());
	}
	
	public Composite_New_Game(Composite parent, int style) {
		super(parent, style);
		
		listeners = new Vector<Composite_New_Game_Listener>();
		woDGame = new WoD_Game();
		
		initialize();
	}

	private void initialize() {
		setSize(new Point(300, 200));
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		setLayout(gridLayout);
		
		get_Label_New_Game();
		get_Text_Game_Title();
		get_Composite_Buttons();
	}

	
	private Label get_Label_New_Game()
	{
		if (label_New_Game == null)
		{
			GridData gridData = new GridData();
			gridData.horizontalSpan = 1;
			
			label_New_Game = new Label(this, SWT.NONE);
			label_New_Game.setText("Crear partida");
			
			label_New_Game.setLayoutData(gridData);
		}
	
		return label_New_Game;
	}
	
	private Text get_Text_Game_Title()
	{
		if (text_Game_Title == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			
			GridData gridData_Composite_Game_Title = new GridData();
			gridData_Composite_Game_Title.grabExcessHorizontalSpace = true;
			gridData_Composite_Game_Title.horizontalAlignment = SWT.FILL;
			
			composite_Game_Title = new Composite(this, SWT.NONE);
			composite_Game_Title.setLayout(gridLayout);
			composite_Game_Title.setLayoutData(gridData_Composite_Game_Title);
			
			label_Game_Title = new Label(composite_Game_Title, SWT.NONE);
			label_Game_Title.setText("Título:");
			
			GridData gridData_Text_Game_Title = new GridData();
			gridData_Text_Game_Title.grabExcessHorizontalSpace = true;
			gridData_Text_Game_Title.horizontalAlignment = SWT.FILL;
			text_Game_Title = new Text(composite_Game_Title, SWT.BORDER);
			text_Game_Title.setLayoutData(gridData_Text_Game_Title);
			
		}
		return text_Game_Title;
	}
	
	
	private Composite get_Composite_Buttons()
	{
		if (composite_Buttons == null)
		{
			GridLayout gridLayout_Composite_Buttons = new GridLayout();
			gridLayout_Composite_Buttons.numColumns = 2;
			
			GridData gridData_Composite_Buttons = new GridData();
			gridData_Composite_Buttons.grabExcessHorizontalSpace = true;
			gridData_Composite_Buttons.horizontalAlignment = SWT.FILL;
			
			composite_Buttons = new Composite(this, SWT.NONE);
			composite_Buttons.setLayout(gridLayout_Composite_Buttons);
			composite_Buttons.setLayoutData(gridData_Composite_Buttons);
			
			/**
			 * Save button
			 */
			GridData gridData_Button_Save = new GridData();
			gridData_Button_Save.grabExcessHorizontalSpace = true;
			gridData_Button_Save.horizontalAlignment = SWT.CENTER;
			
			button_Accept = new Button(composite_Buttons, SWT.PUSH);
			button_Accept.setLayoutData(gridData_Button_Save);
			button_Accept.setText("Aceptar");
			
			final Composite_New_Game compositeNewGame = this;
			button_Accept.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					
					/**
					 * Save inputs to configuration object
					 */
					fill_GameData();
					
					Composite_New_Game_Event event = new Composite_New_Game_Event(compositeNewGame);
					event.setEventType(Composite_New_Game_Event.Event_Types.ACCEPT);
					fire_Composite_New_Game_Event(event);
				}
			});
		
			
			/**
			 * Cancel button
			 */
			GridData gridData_Button_Cancel = new GridData();
			gridData_Button_Cancel.grabExcessHorizontalSpace = true;
			gridData_Button_Cancel.horizontalAlignment = SWT.CENTER;

			button_Cancel = new Button(composite_Buttons, SWT.PUSH);
			button_Cancel.setLayoutData(gridData_Button_Cancel);
			button_Cancel.setText("Cancelar");
			
			button_Cancel.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_New_Game_Event event = new Composite_New_Game_Event(compositeNewGame);
					event.setEventType(Composite_New_Game_Event.Event_Types.CANCEL);
					fire_Composite_New_Game_Event(event);
				}
			});
			
		}
		
		return composite_Buttons;
	}
	
	public void send_Http_Message(String http_dst, String http_port, String UserName, String ServerHost, String ServerPort, String GameTitle) throws Exception
	{
		String gameid = null;
		gameid = WoD_HttpMessage.NewGame(http_dst, http_port, UserName, ServerHost, ServerPort, GameTitle);
		if(!gameid.equals("-1"))
			woDGame.setID_Game(Long.parseLong(gameid));
				
	}
	
	/**
	 * Shows error message
	 * @param error
	 */
	private void show_Error(String error)
	{
		MessageBox messageBox = new MessageBox(getShell(), SWT.ICON_ERROR | SWT.OK);
		messageBox.setText("Error");
		messageBox.setMessage(error);
		messageBox.open();
	}
	
}
